#include "render.hh"
#include "gl.hh"

namespace gl
{
	namespace render
	{
		void square(float x1, float y1, float x2, float y2, bool solid)
		{
			gl::begin((solid ? gl::_quads : gl::_line_loop));
			gl::vertex2f(x1, y1);
			gl::vertex2f(x2, y1);
			gl::vertex2f(x2, y2);
			gl::vertex2f(x1, y2);
			gl::end();
		}

		void cube(bool solid)
		{
			static constexpr gl::float_t verts[] = {
				// back 4 vertices
				-0.5, -0.5, -0.5,
				+0.5, -0.5, -0.5,
				+0.5, +0.5, -0.5,
				-0.5, +0.5, -0.5,
				// front 4 vertices
				-0.5, -0.5, +0.5,
				+0.5, -0.5, +0.5,
				+0.5, +0.5, +0.5,
				-0.5, +0.5, +0.5,
			};
			static constexpr gl::ubyte_t faces[] = {
				0, 1, 2, 3,		// back
				4, 5, 6, 7,		// front
				0, 4, 7, 3,		// left
				1, 2, 6, 5,		// right
				0, 1, 5, 4,		// bottom
				2, 3, 7, 6,		// top
			};
			static constexpr gl::ubyte_t wire[] = {
				0, 1, 2, 3,		// back
				4, 5, 6, 7,		// front
				0, 4,
				1, 5,
				2, 6,
				3, 7,
			};
			static gl::uint_t lists[2];
			static bool first = true;
			if (first) {
				first = false;
				lists[false] = gl::gen_lists(2);
				lists[ true] = lists[false]+1;

				gl::new_list(lists[true], gl::_compile);
				gl::enable_client_state(gl::_vertex_array);
				gl::vertex_pointer(3, gl::_float, 0, verts);
				gl::draw_elements(gl::_quads, 6*4, gl::_unsigned_byte, faces);
				gl::disable_client_state(gl::_vertex_array);
				gl::end_list();

				gl::new_list(lists[false], gl::_compile);
				gl::enable_client_state(gl::_vertex_array);
				gl::vertex_pointer(3, gl::_float, 0, verts);
				gl::draw_elements(gl::_line_loop, 4, gl::_unsigned_byte, &wire[0]);
				gl::draw_elements(gl::_line_loop, 4, gl::_unsigned_byte, &wire[4]);
				gl::draw_elements(gl::_lines    , 8, gl::_unsigned_byte, &wire[8]);
				gl::disable_client_state(gl::_vertex_array);
				gl::end_list();
			}
			gl::call_list(lists[solid]);
		}
	}
}

